<cfif NOT directoryexists("#request.basepath#blog/temp")>
	<cfdirectory action="create" directory="#request.basepath#blog/temp">
</cfif>
<cfif len(trim(form.b_title)) GT 0 AND len(trim(form.b_content)) GT 0>
	<cfset filename = replace(form.b_title, " ", "_", "ALL")>
	<cfset filename = lcase(filename)>
    <cfset filename = replace(filename, " ", "-", "ALL")>
    <cfset filename = replace(filename, "/", "", "ALL")>
    <cfset filename = replace(filename, "\", "", "ALL")>
    <cfset filename = replace(filename, "*", "", "ALL")>
    <cfset filename = replace(filename, "=", "", "ALL")>
    <cfset filename = replace(filename, "!", "", "ALL")>
    <cfset filename = replace(filename, "##", "", "ALL")>
    <cfset filename = replace(filename, "%", "", "ALL")>
    <cfset filename = replace(filename, "(", "", "ALL")>
    <cfset filename = replace(filename, ")", "", "ALL")>
    <cfset filename = replace(filename, "%", "", "ALL")>
    <cfset filename = replace(filename, "^", "", "ALL")>
    <cfset filename = replace(filename, "$", "", "ALL")>
    <cfset filename = replace(filename, "@", "", "ALL")>
    <cfset filename = replace(filename, "<", "", "ALL")>
    <cfset filename = replace(filename, ">", "", "ALL")>
    <cfset filename = replace(filename, ".", "", "ALL")>
    <cfset filename = replace(filename, "?", "", "ALL")>
	<cfset filename = "#filename#.cfm">
	
    <cfset b_year = dateformat(b_postedon, "yyyy")>
    <cfset b_month = dateformat(b_postedon, "mm")>

    <cffile action="write" output="#form.b_content#" file="#request.basepath#blog/temp/#filename#"> 

	<cfset blog_postdate = "#dateformat(b_postedon, 'mm/dd/yyyy')# #b_hour#:#b_minute# #b_ampm#">

	<cfquery name="qdup" datasource="#request.dsn#">
    SELECT * FROM blog_posts
    WHERE b_file = <cfqueryparam value="#filename#" cfsqltype="cf_sql_varchar">
    </cfquery>
    
    <cfif qdup.recordcount IS 0>
        <cfquery name="insertblogentry" datasource="#request.dsn#">
            INSERT INTO blog_posts
            (b_title, b_file, b_month, b_year, b_day, b_postedon, b_brief, status)
            VALUES
            ('#form.b_title#', '#filename#', #b_month#, #b_year#, 1, #createodbcdatetime(blog_postdate)#, '#form.b_brief#', 0)
        </cfquery>
    <cfelse>
        <cfquery name="updateentry" datasource="#request.dsn#">
        UPDATE blog_posts
        SET b_title = '#form.b_title#',
        b_file = '#filename#',
        b_postedon = #createodbcdatetime(blog_postdate)#,
        b_brief = '#form.b_brief#'
        WHERE b_file = <cfqueryparam value="#filename#" cfsqltype="cf_sql_varchar">
        AND status = <cfqueryparam value="0" cfsqltype="cf_sql_integer">
        </cfquery>
	</cfif>
    Blog entry has been autosaved.
</cfif>